package com.wu.qqserver.service;

import java.util.HashMap;
import java.util.Set;

/*

该类用于管理和客户端 通讯的线程
 */
public class ManageServerConnectClientThread {

    //我们把多个线程放入到hashMap中  key用用户ID  value就是线程
    private static HashMap<String,ServerConnectClientThread> hm = new HashMap<>();


    //将某个线程加入集合
    public static void addServerConnectClientThread(String userId,
                                                    ServerConnectClientThread serverConnectClientThread){
        hm.put(userId, serverConnectClientThread);
    }

    //通过Userid将某个线程取出
    public static ServerConnectClientThread getServerConnectClientThread(String userId){
        return hm.get(userId);
    }


    //返回在线用户列表
    public static String getOnLineUserList(){
        Set<String> keySet = hm.keySet();
        String str = "";
        for (String s : keySet) {
            str+= s+" ";
        }
        return str;
    }

    //从集合中移除一个线程
    public static void logOutServerConnectClientThread(String userId) {
        hm.remove(userId);
    }



    //判断某个用户是否在线
    public static boolean userIdIsOnLine(String userId){
        return hm.containsKey(userId);
    }
}
